/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.peer.ui.async.gui;

import javax.swing.JCheckBox;

import org.ourgrid.common.spec.worker.WorkerSpec;
import org.ourgrid.common.spec.worker.WorkerSpecConstants;
import org.ourgrid.peer.ui.async.client.PeerAsyncInitializer;
import org.ourgrid.peer.ui.async.util.SDFDefaults;

/**
 * AddWorkerDialog provides a simple mechanism for the user to add a Worker.
 */
public class AddWorkerDialog extends javax.swing.JDialog {
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    
    /** Creates new form AddWorkerDialog 
     * @param componentClient */
    public AddWorkerDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        workerRegistrationPanel1 = new org.ourgrid.peer.ui.async.gui.WorkerRegistrationPanel();
        classAdSyntax = new JCheckBox( "Use ClassAd Syntax", false );

        setTitle("Add Worker");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        okButton.setText("OK");
        okButton.setName("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(100, Short.MAX_VALUE)
                .add(okButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cancelButton)
                .addContainerGap())
                .add(classAdSyntax)
            .add(workerRegistrationPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        layout.linkSize(new java.awt.Component[] {cancelButton, okButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(workerRegistrationPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                .add(classAdSyntax)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * Confirms the addition of a new Worker.
     * @param evt The event the represents the clicking of the ok button.
     */
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
    	addWorker();
    	doClose(RET_OK);
    }//GEN-LAST:event_okButtonActionPerformed
    
	/**
	 * Cancel and close this dialog.
	 * @param evt The event the represents the clicking of the cancel button.
	 */
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed
    
    /** Closes this dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog
    
    /**
     * Close this dialog using the status provided.
     * @param retStatus The status of this dialog.
     */
    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }
    
    /**
     * Adds a new Worker to the SDF File.
     */
    public void addWorker(){
    	 WorkerSpec workerSpec = createWorkerSpec();
    	 PeerAsyncInitializer.getInstance().getComponentClient().addWorker(workerSpec);
    }

    /**
     * Creates a new Worker Spec, that have the user and server
     * provided.
     * @return The WokerSpec created.
     */
	private WorkerSpec createWorkerSpec() {
		WorkerSpec workerSpec;
		if(classAdSyntax.isSelected()){
			workerSpec = new WorkerSpec("[]");
		}else{
			workerSpec = new WorkerSpec();
		}
		String user = workerRegistrationPanel1.getUser();
		String server = workerRegistrationPanel1.getServer();
		workerSpec.putAttribute( WorkerSpecConstants.USERNAME, user );
		workerSpec.putAttribute( WorkerSpecConstants.SERVERNAME, server );
		workerSpec.putAttribute( WorkerSpec.ATT_COPY_FROM, SDFDefaults.COPY_FROM );
		workerSpec.putAttribute( WorkerSpec.ATT_COPY_TO, SDFDefaults.COPY_TO );
		workerSpec.putAttribute( WorkerSpec.ATT_REM_EXEC, SDFDefaults.REM_EXEC );
		
		return workerSpec;
	}
    
    /**
     * Main method.
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AddWorkerDialog dialog = new AddWorkerDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton okButton;
    private javax.swing.JCheckBox classAdSyntax;
    private org.ourgrid.peer.ui.async.gui.WorkerRegistrationPanel workerRegistrationPanel1;
    // End of variables declaration//GEN-END:variables
    
    private int returnStatus = RET_CANCEL;
}
